<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

class temporal extends entity {

	var $id;
	var $expire;
	var $options;
	var $extra;
	var $teamid;
	var $filler;
	
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new temporal;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get($exp = true) {
		$sql = "SELECT * FROM ".TEMPORAL." WHERE id = ".util::str($this->id);
		if ($exp) {
			$sql .= " AND expire > ".util::str(util::now());
		}
		return $sql;
	}

	public function listBy() {
		$sql = "SELECT * FROM ".TEMPORAL;
		$filter="";
		if ($this->options!=null) {
			$filter .= addMFilter($this->options,"options");
		}
		if ($this->teamid!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->teamid,"teamid");
		}
		if ($this->extra!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->extra,"extra");
		}
		if ($this->filler!=null) {
			if ($filter!="") $filter.=" AND ";
			$filter .= addMFilter($this->filler,"filler");
		}				
		if ($filter != "") $sql .= " WHERE " .$filter;
		return $sql;
	}
	
	public function cleanUp() {
		$expire = util::formatDate(time()-(DB_CLEANUP*60*60));
		$sql = "DELETE FROM ".TEMPORAL." WHERE expire<=".util::str($expire);
		return $sql;
	}
	
	public function listExpired($opt) {
		$sql = "SELECT * FROM ".TEMPORAL." WHERE expire<=".util::str(util::now())." AND options = ".util::str($opt);
		return $sql;
	}
	
	public function insert() {
		if ($this->expire==null) $this->expire = util::expireDate(INVITATION_EXPIRE);
		$sql = "INSERT INTO ".TEMPORAL." (id, expire, options, extra, teamid, filler) ";
		$sql .= "VALUES (";
		$sql .=	util::str($this->id);
		$sql .=	",".util::str($this->expire);
		$sql .=	",".util::str($this->options);
		$sql .=	",".util::str($this->extra);
		$sql .=	",".util::str($this->teamid);
		$sql .=	",".util::str($this->filler);
		$sql .= ")";
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".TEMPORAL. " WHERE id=".util::str($this->id);
		return $sql;
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->expire=$array['expire'];
		$this->options=$array['options'];
		$this->extra=$array['extra'];
		$this->teamid=$array['teamid'];
		$this->filler=$array['filler'];
	}
	
}

?>
